/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package javafx191labs.ssheypak.common.data;

import java.util.HashMap;
import java.util.Map;
import javafx191labs.ssheypak.common.exception.DataInputException;

/**
 * Class "manages" connections to data store.
 * Simple realization of singleton.
 *
 * @author SSheypak
 */
public class DataStoreManager {

    /** Single instance. */
    private static Map<String, InMemoryDataStore> dataStores = new HashMap<String, InMemoryDataStore>();

    /**
     * Get "connection".
     * @param instance is a instance with desired data store type
     * @return exsiting data store or new instance.
     * @throws DataInputException if instance doesn't fit requirements.
     */
    public static InMemoryDataStore getDataStore(Entity instance) throws DataInputException{
        if(instance == null){
            throw new DataInputException("Невозможно создать хранилище данных, т.к. тип данных не указан.");
        }
        InMemoryDataStore typedDataStore = dataStores.get(instance.getClass().getName());
        if(typedDataStore == null){
            typedDataStore = new InMemoryDataStore();
            dataStores.put(instance.getClass().getName(), typedDataStore);
        }
        return typedDataStore;
    }
}
